﻿using UnityEngine;
using System.Collections;


[ExecuteInEditMode]
public class MagicWaveGage : MonoBehaviour 
{

	public UIPanel						PanelLink = null;
	public UITexture					NGUITextureLink = null;
	public GameObject					RootFxLink = null;
	

	public Texture[]					FillGageTextureArray = null;
	public Texture[]					MaskTextureArray = null;
	public Texture[]					WaveLineTextureArray = null;

	public float						Amount = 1.0f;
	public float						FrameAniSpeed = 0.3f;


	int									mCurFrameNum = 0;
	float								mFrameAccuTime = 0.0f;
		
	// Use this for initialization
	void Start () 
	{
		if( this.NGUITextureLink == null )
		{
			this.NGUITextureLink = this.GetComponent( typeof( UITexture ) ) as UITexture;
		}
		
		if( ( this.NGUITextureLink == null ) ||
			( this.FillGageTextureArray == null ) )
		{
			this.enabled = false;
			return;
		}

		if ( this.PanelLink != null )
		{
			this.PanelLink.alpha = 1.0f;
			this.PanelLink.Refresh ();
			//this.PanelLink.alpha = 0.0f;
		}
	
		/*
		if( this.NGUITextureLink.material != null )
		{
			this.NGUITextureLink.material.hideFlags = HideFlags.HideAndDontSave;
			if( this.NGUITextureLink.mainTexture != null )
				this.NGUITextureLink.mainTexture.hideFlags = HideFlags.HideAndDontSave;
		}
		 */

		this.mFrameAccuTime = this.FrameAniSpeed;			
		//this.Amount = 0.5f;
	}

	// Update is called once per frame
	void Update () 
	{
		
		this.mFrameAccuTime -= Time.deltaTime;
		if( this.mFrameAccuTime < 0.0f )
		{		
			++this.mCurFrameNum;
			if( this.FillGageTextureArray.Length <= this.mCurFrameNum )
			{
				this.mCurFrameNum = 0;
			}


			//	이것은 편법!!!
			//this.NGUITextureLink.enabled = false;
			this.NGUITextureLink.mainTexture = this.FillGageTextureArray[ this.mCurFrameNum ];
			//this.NGUITextureLink.enabled = true;		
			this.NGUITextureLink.material.SetTexture ( "_MainTex", this.FillGageTextureArray[ this.mCurFrameNum ] );
			this.NGUITextureLink.material.SetTexture ( "_AlphaTex", this.MaskTextureArray[ this.mCurFrameNum ] );
			this.NGUITextureLink.material.SetTexture ( "_WaveTex", this.WaveLineTextureArray[ this.mCurFrameNum ] );
			this.NGUITextureLink.material.SetFloat ( "_Amount", this.Amount );
			this.NGUITextureLink.SetDirty();
			
			this.mFrameAccuTime = this.FrameAniSpeed;			
		}
		
	}

	public void ShowFX()
	{
		if ( this.RootFxLink != null )
		{
			ParticleSystem ps = this.RootFxLink.particleSystem;
			if ( ps != null )
			{
				ps.Play ( true );
			}
		}		
	}
}
